package com.yjd.back.user;

import com.yjd.comm.bank.model.BankModel;
import com.yjd.comm.bank.service.IBankService;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.enums.CreditIndustryLevel;
import com.yjd.comm.funds.enums.FundsIncomeEnum;
import com.yjd.comm.funds.enums.FundsRefTableEnum;
import com.yjd.comm.funds.enums.FundsTypeEnum;
import com.yjd.comm.funds.model.FundsDataModel;
import com.yjd.comm.funds.service.IFundsDataService;
import com.yjd.comm.funds.service.IFundsService;
import com.yjd.comm.user.dto.UserSession;
import com.yjd.comm.user.dto.ContactInfo;
import com.yjd.comm.user.model.UserDataModel;
import com.yjd.comm.user.model.UserModel;
import com.yjd.comm.user.dto.WorkInfo;
import com.yjd.comm.user.service.IUserDataService;
import com.yjd.comm.user.service.IUserService;
import com.yjd.comm.util.*;
import com.yjd.comm.zhima.model.ZhimaCreditDataModel;
import com.yjd.comm.zhima.service.IZhimaCreditService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by yjd-wap on 2017/7/10.
 */
@Controller
@Scope("prototype")
@RequestMapping("/user/")
public class UserControl extends BaseControl {

    @Resource
    private IUserService userService;

    @Resource
    private IUserDataService userDataService;

    @Resource
    private IBankService bankService;

    @Resource
    private IZhimaCreditService zhimaCreditService;

    @Resource
    private IFundsService fundsService;

    @Resource
    private IFundsDataService fundsDataService;

    /**
     * 用户列表列表
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("list")
    public String list() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);
        paramMap.put("sort"," create_time desc ");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                userService.getPageModel(paramMap, DbWREnums.READ),
                paramMap, ServletUtil.getUrl("/user/list"));
        List<UserModel> userList = (List<UserModel>) pager.getPagerModel().getDataList();
        List<Long> userIdList = FrameUtil.getBeanFieldValues(userList, "user_id");
        Map<Long, UserDataModel> userDataModelMap = null;
        if (userIdList != null && userList.size() > 0) {
            userDataModelMap = userDataService.getModelMapByIds(userIdList, DbWREnums.READ);
        }

        for (UserModel userModel : userList) {
            UserDataModel userDataModel = userDataModelMap.get(userModel.getUser_id());
            if (userDataModel != null) {
                userModel.getStrMap().put("idName", userDataModel.getId_name());
                userModel.getStrMap().put("idCard", userDataModel.getId_number());
            }
        }
        request.setAttribute("pager", pager);
        return "user.list";
    }

    /**
     * 用户详情
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("detail/{user_id}")
    public String detail(@PathVariable("user_id") Long user_id) throws Exception {
        if (user_id == null) {
            FrameUtil.throwBaseException1("参数不能为空");
        }
        UserSession userSession = userService.getUserSession(user_id);
        if (userSession == null) {
            FrameUtil.throwBaseException1("该用户不存在");
        }
        BankModel bankModel = bankService.getModelById(user_id, DbWREnums.READ);
        if (bankModel != null) {
            userSession.getStrMap().put("bank_name", bankModel.getBank_name());
            userSession.getStrMap().put("bank_no", bankModel.getBank_card_number());
            userSession.getStrMap().put("idName", bankModel.getId_name());
            userSession.getStrMap().put("idCard", bankModel.getId_number());
            userSession.getStrMap().put("bank_mobile", bankModel.getBank_mobile());
            userSession.getStrMap().put("bank_time", bankModel.getCreate_time());
        }

        UserDataModel userDataModel = userDataService.getModelById(user_id, DbWREnums.READ);
        if (userDataModel != null) {
            if (userDataModel.getJob_info() != null) {
                WorkInfo workInfo = StringUtil.getObject(userDataModel.getJob_info(), WorkInfo.class);
                if (workInfo != null) {
                    userSession.getStrMap().put("company", workInfo.getCompany());
                    userSession.getStrMap().put("companyType", workInfo.getCompanyType());
                    userSession.getStrMap().put("companyLocation", workInfo.getCompanyLocation());
                    userSession.getStrMap().put("residenceLocation", workInfo.getResidenceLocation());
                    userSession.getStrMap().put("duration", workInfo.getDuration());
                }
            }

            if (userDataModel.getContacts() != null) {
                ContactInfo contactInfo = StringUtil.getObject(userDataModel.getContacts(), ContactInfo.class);
                if (contactInfo != null) {
                    userSession.getStrMap().put("contacName", contactInfo.getIdName());
                    userSession.getStrMap().put("contacRelation", contactInfo.getRelation());
                    userSession.getStrMap().put("contacMoblie", contactInfo.getMobile_number());
                }
            }
        }

        // 用户芝麻信息
        ZhimaCreditDataModel zhimaInfo = zhimaCreditService.zhimaData(userSession.getUser_id());
        zhimaInfo.getStrMap().put("creditLabel", CreditIndustryLevel.parse(zhimaInfo.getIndustry_level()).getLabel());
        request.setAttribute("zhimaInfo", zhimaInfo);

        request.setAttribute("user", userSession);
        return "user.detail";
    }

    @RequestMapping("freeze/{user_id}")
    public void freeze(@PathVariable("user_id") Long user_id) throws Exception {
        if (user_id == null) {
            FrameUtil.throwBaseException1("参数不能为空");
        }
        UserModel model = new UserModel();
        model.setUser_id(user_id);
        model.setUser_status(1);
        userService.update(model);
        successMsg("冻结成功");
    }

    @RequestMapping("unFreeze/{user_id}")
    public void unFreeze(@PathVariable("user_id") Long user_id) throws Exception {
        if (user_id == null) {
            FrameUtil.throwBaseException1("参数不能为空");
        }
        UserModel model = new UserModel();
        model.setUser_id(user_id);
        model.setUser_status(0);
        userService.update(model);
        successMsg("解冻成功");
    }

    /**
     * 用户资金
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("fund")
    public String userFund() throws Exception {
        Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);
        paramMap.put("sort"," user_id desc ");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                fundsService.queryUserFunds(paramMap, DbWREnums.READ), paramMap, ServletUtil.getUrl("/user/fund"));
        request.setAttribute("pager", pager);
        return "user.fund";
    }

    /**
     * 用户资金流水
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("fund/data")
    public String userFundData(Long user_id) throws Exception {
        Map<String, Object> paramMap = ServletUtil.getPageParameterMap(request);
        paramMap.put("sort"," update_time desc, id desc ");
        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                fundsDataService.getPageModel(paramMap, DbWREnums.READ),
                paramMap, ServletUtil.getUrl("/user/fund/data"));

        FundsDataModel fdm = null;
        for (Object obj : pager.getPagerModel().getDataList()) {
            fdm = (FundsDataModel)obj;
            fdm.getStrMap().put("income", FundsIncomeEnum.parse(fdm.getIncome()).getLabel());
            fdm.getStrMap().put("type", FundsTypeEnum.parse(fdm.getType()).getLabel());
            fdm.getStrMap().put("ref_table", FundsRefTableEnum.parse(fdm.getRef_table()).getLabel());
            fdm.getStrMap().put("update_time", DateUtil.timestampToDateString(fdm.getUpdate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
        }

        request.setAttribute("pager", pager);
        return "user.fund.data";
    }

}
